package com.itheima.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.SmsFlashPromotionMapper;
import com.itheima.mapper.UmsRoleMapper;
import com.itheima.pojo.PmsProductAttributeCategory;
import com.itheima.pojo.SmsFlashPromotion;
import com.itheima.pojo.UmsRole;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

@DubboService
public class SmsFlashPromotionImpl implements SmsFlashPromotionApi{
    @Autowired
    private SmsFlashPromotionMapper smsFlashPromotionMapper;
    /**
     * 添加活动
     */
    @Override
    public Boolean create(SmsFlashPromotion smsFlashPromotion) {
        //先进行查询 类型名称是否重复
        LambdaQueryWrapper<SmsFlashPromotion> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SmsFlashPromotion::getTitle,smsFlashPromotion.getTitle());
        SmsFlashPromotion pmsBrand1 = smsFlashPromotionMapper.selectOne(wrapper);
        //如果存在 则重复 返回 FALSE
        if(!ObjectUtils.isEmpty(pmsBrand1)){
            return false;
        }
        //走新增
        smsFlashPromotionMapper.insert(smsFlashPromotion);
        return true;

    }

    /**
     * 删除活动
     * @param id
     */
    @Override
    public void delete(Integer id) {
        smsFlashPromotionMapper.deleteById(id);
    }

    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public IPage<SmsFlashPromotion> list(Integer pageNum, Integer pageSize, String keyword) {
        IPage<SmsFlashPromotion> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsFlashPromotion> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!ObjectUtil.isEmpty(keyword),SmsFlashPromotion::getTitle,keyword);
        return smsFlashPromotionMapper.selectPage(page,wrapper);
    }

    /**
     * 修改上下线状态
     * @param id
     * @param status
     */
    @Override
    public void status(Integer id, Integer status) {
        LambdaQueryWrapper<SmsFlashPromotion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SmsFlashPromotion::getId,id);
        SmsFlashPromotion smsFlashPromotion = smsFlashPromotionMapper.selectOne(lqw);
        if (ObjectUtil.isNotNull(smsFlashPromotion)){
            smsFlashPromotion.setStatus(status);
            update(id,smsFlashPromotion);
        }
    }

    /**
     * 编辑活动
     * @param id
     * @param smsFlashPromotion
     */
    @Override
    public void update(Integer id, SmsFlashPromotion smsFlashPromotion) {
        LambdaQueryWrapper<SmsFlashPromotion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SmsFlashPromotion::getId, id);
        smsFlashPromotionMapper.update(smsFlashPromotion,lqw);
    }

    /**
     * 活动详情
     * @param id
     * @return
     */
    @Override
    public SmsFlashPromotion findById(Integer id) {
        SmsFlashPromotion smsFlashPromotion = smsFlashPromotionMapper.selectById(id);
        return smsFlashPromotion;
    }
}
